package com.vincent.kdp.Activity;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.vincent.kdp.PostUrl;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/30.
 */

public class GlobalService  extends Service{
    private static final String TAG = "GlobalService";
    private RequestQueue requestQueue;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        Log.i(TAG, "GlobalService-onCreate");
        super.onCreate();
        requestQueue= Volley.newRequestQueue(this);
       //开始一个新的线程
        Runnable  runnable=new Runnable() {
            @Override
            public void run() {

                while(true)
                {
                    setdevice();
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
       new Thread(runnable).start();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //获得手机唯一标识
    private  String getPhoneIMEI()
    {
        TelephonyManager telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String  phoneIMEI=telephonyManager.getDeviceId();
        Log.i("PKIO",phoneIMEI);
        return phoneIMEI;
    }

    //请求数据
    private  void setdevice()
    {
        StringRequest stringRequest=new StringRequest(Request.Method.POST, PostUrl.ACTIVR_HEART, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String>map=new HashMap<String,String>();
                map.put("number",getPhoneIMEI());
                return map;
            }
        };
       // stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, 1.0f));
        requestQueue.add(stringRequest);
    }

}
